﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Transactions;
using InsideDL.MISInterface;
using MISInterface;

namespace InsideDL.MISData
{
    public class DataARTM973:DataMatrix//,iUpload
    {
        //public System.ComponentModel.BackgroundWorker backgroundWorker1 = null;

        public int Max
        {
            get
            {
                return lines.Length;
            }
        }

        //int highestPercentageReached = 0;


        public DataARTM973() { }

        public DataARTM973(string p_path)
        {
            lines = System.IO.File.ReadAllLines(p_path, Encoding.Default);
        }

        
        public void Upload(string p_path)
        {
            lines = System.IO.File.ReadAllLines(p_path, Encoding.Default);
            //上傳前先確認是否已經做過
            check(Utility.BeforeMonthLastDay(Utility.GetCurrentYM(DateTime.Now)));
        
            //string[] flds,paraNames;
            string[] paraNames = new []{ 
                "yyyymm","branch","acc_no","acc_name","amount"};

            string sql = @"
INSERT INTO ARTM973(yyyymm,branch,acc_no,acc_name,amount) 
VALUES(@yyyymm,@branch,@acc_no,@acc_name,@amount)
";          char[] splitMark = new []{' '};
            DbType[] dbTypes = new []{DbType.String,DbType.String,DbType.String
                ,DbType.String,DbType.String};
            base.upLoad(sql, paraNames, splitMark, dbTypes);            
        }

        /// <summary>
        /// 如果要新增前資料已存在，先刪再新增
        /// </summary>
        /// <param name="p_ym"></param>
        protected override void check(string p_ym)
        {
            string sql = @"
SELECT COUNT(*) AS CNT FROM ARTM973 WHERE yyyymm = @p_ym
";
            string sDate = string.Format("{0}-{1}-{2}",p_ym.Substring(0,4)
                ,p_ym.Substring(4,2),p_ym.Substring(6,2));
            System.Data.Common.DbCommand cmd = getCommand();
            cmd.CommandText = sql;
            cmd.Parameters.Add(getParameter("p_ym", DbType.String, sDate, cmd.CreateParameter()));            
            System.Data.DataTable dt = getSQLData(cmd);
            if (!dt.Rows[0]["cnt"].ToString().Equals("0"))
            {
                sql = "DELETE FROM ARTM973 WHERE yyyymm=@p_ym";
                cmd = getCommand();
                cmd.Parameters.Add(getParameter("p_ym", DbType.Date, sDate, cmd.CreateParameter()));
                cmd.CommandText = sql;
                executeSQL(cmd);
            }
            
        }

     
    }
}
